package com.liangzong.betspider.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDateTime;

import static com.liangzong.betspider.utils.SpiderInfoUtil.*;

@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class RefreshProxyScheduleTask {
    //暂不启用
    //每天上午午5点，12点 0 0 5,12 * * ?
    //@Scheduled(cron = "0 0 5,12 * * ?")
    private void refreshTasks() {
        System.err.println("执行刷新代理定时任务时间: " + LocalDateTime.now());
        execRefreshProxyIP();
        setRefreshState();
    }

    private void execRefreshProxyIP() {
        try {
            String line;
            String cmd = "cmd /c hsdl -c -L \"杭州电信5\"";
            java.lang.Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), "gbk"));//使用gbk编码解决输出乱码问题
            while ((line = bufferedReader.readLine()) != null) {
                log.info(line);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
